package com.asiainfo.dao;
/*
 * 
 * 预订课程数据库操作类
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.asiainfo.bean.Subscribe;
import com.asiainfo.database.JdbcTemplete;
import com.asiainfo.database.ResultSetHandler;

public class Subscribe_Dao {
	//增加预订课程
	public int addsubscribe(Subscribe subscribe){
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "insert into subscribe(subscribe_course,subscribe_vip,subscribe_date,subscribe_isdelete) values (?,?,?,?)";
		return jt.update(sql, subscribe.getSubscribe_course(),subscribe.getSubscribe_vip(),(int)new Date().getTime(),subscribe.getSubscribe_isdelete());
	}
	//取消预定课程
	public Subscribe findsubscribeByVipandSchedule(int vip_id,int course_id){
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "select subscribe_course,subscribe_vip,subscribe_date,subscribe_isdelete,subscribe_id from subscribe where subscribe_vip=? and subscribe_course=? and subscribe_isdelete=0";
		Subscribe subscribe = (Subscribe) jt.quary(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				Subscribe subscribe = null;
				if (rs.next()) {
					subscribe = new Subscribe();
					subscribe.setSubscribe_course(rs.getInt(1));
					subscribe.setSubscribe_vip(rs.getInt(2));
					subscribe.setSubscribe_date(rs.getInt(3));
					subscribe.setSubscribe_isdelete(rs.getInt(4));
					subscribe.setSubscribe_id(rs.getInt(5));
				}
				return subscribe;
			}
		}, vip_id,course_id);
		return subscribe;
	}
	//更新预订信息
	public int updatesubscribe(Subscribe subscribe){
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "update subscribe set subscribe_course=?,subscribe_vip=?,subscribe_date=?,subscribe_isdelete=? where subscribe_id=?";
		return jt.update(sql, subscribe.getSubscribe_course(),subscribe.getSubscribe_vip(),subscribe.getSubscribe_date(),subscribe.getSubscribe_isdelete(),subscribe.getSubscribe_id());
	}
}
